<?php
namespace app\api\controller;
use think\Db;
use app\admin\model\Config;


class News extends App
{
    
   public function zhiyuan_export(){
        
        $user_id = $this->request->param("zid",0,'intval');;
		$id = $this->request->param("tid",0,'intval');
		if(empty($id)){
			return $this->show(2001,"缺少参数id!", '');
		}
		$zhiyuan_info = Db::name("member_zhiyuan")->where('user_id',$user_id)->where('id',$id)->find();
		if(empty($zhiyuan_info)){
			return $this->show(2001,"未找到数据!", '');
		}
		$title = $zhiyuan_info['title'];
		$cj_score = $zhiyuan_info['cj_score'];
		$batch = ($zhiyuan_info['batch']==1)?'本科':'专科';
		$cj_subjects = $zhiyuan_info['cj_subjects'];
		
		$zhiyuan_json = json_decode($zhiyuan_info['zhiyuan_json'],true);
		
		$realname = Db::name("member")->where('id',$user_id)->value('realname');
		$province = Db::name("area")->where('id',$zhiyuan_info['province_id'])->value('name');
		$title = $province.date('Y').'普通高校招生志愿预填表';
		$html = '<div style="width:1280px;"><h3 style="text-align: center;">'.$title.'</h3>';
		$html .= '<table cellspacing="0" cellpadding="0" border="1" style="text-align:center;width:280px;margin-bottom:20px;margin-bottom:20px;margin-top:50px;">';
		$html .= '<tr><td>姓名</td><td>'.$realname.'</td></table>';
		
		$html .= '<table cellspacing="0" cellpadding="0" border="1" style="text-align:center;width:1280px;">';
		$html .= '<tr><td style="width:80px;">志愿序号</td><td style="width:200px;">院校代码</td><td style="width:200px;">院校名称</td><td style="width:800px;"><table style="text-align:center;"><tr><td style="width:80px;">序号</td><td style="width:110px;">专业组代码</td><td style="width:110px;">专业代码</td><td style="width:500px;">专业名称</td></tr></table></td></tr>';
		foreach($zhiyuan_json as $k=>$v){
			
		
			$html .= '<tr>
			  <td>'.($k+1).'</td>
			  <td>'.$v['college_name'].'</td>
			  <td>'.$v['school_code'].'</td>
			  <td><table cellspacing="0" cellpadding="0" border="1" style="text-align:center;">';
				foreach($v['major_list'] as $k1=>$v1){		
					$subject_group = !empty($v1['subject_group'])?'组：'.$v1['subject_group']:'';
					$html .='<tr>
				  <td style="width:80px;">'.($k1+1).'</td>
				  <td style="width:110px;">'.$subject_group.'</td>
				  <td style="width:110px;">'.$v1['major_code'].'</td>
				  <td style="width:500px;">'.$v1['major_name'].'</td>
				</tr>';
				}
				
			$html .= '</table>
			  </td>
			</tr>'; 
		}	
		$html .='</table></div>';
		//echo $html;die;
		header("Content-Type: application/vnd.ms-excel");
		header("Content-Disposition: attachment; filename=志愿表.xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		
		// 输出内容（解决中文乱码）
		echo iconv('UTF-8', 'GBK//IGNORE', '<html><head><meta charset="UTF-8"></head><body>');
		echo $html;
		echo '</body></html>'; 
		
    } 
    public function index()
    {
        $pagesize = 10;
        $page = $this->request->get("page",0,'intval'); 
		
		
		$map['status'] = 1;
        $count = Db::name("article")->where($map)->count();
		$list = Db::name("article")->where($map)->field('id,title,remark,writer,views,photo,create_time')->order('id desc')
			->page($page,$pagesize)->select();  
			
		$list = array_map(function ($rs){
            $rs["create_time"] = !empty($rs["create_time"])?date('Y-m-d H:i',$rs["create_time"]):'';
			
			$photos = explode(',',$rs['photo']);
			$photos = array_filter($photos);
			foreach($photos as $kp=>$vp){ 
				//$photos[$kp] = config('app_host').$vp;
				$photos[$kp] = $vp;
			}
			$rs['photo'] = $photos;
			$rs['photo_count'] = count($photos);
			
            return $rs;
        }, $list);		
			
        $pages = ceil($count/$pagesize);
        $data = array('list'=>$list,'count'=>$count,'pages'=>$pages,'pagesize'=>$pagesize);
		return $this->show(1,"成功", $data);
		
    }
    
    
    public function detail()
    {
        
        $id = $this->request->get("id",0,'intval'); 
        if(empty($id)){
            return $this->show(2001,"缺少id!", '');
        }
        $info = Db::name("article")->field('id,title,writer,views,keyword,content,create_time')->where('id',$id)->where('status',1)->find();
        if(empty($info)){
            return $this->show(2001,"资讯不存在或已下架!", '');
        }
		Db::name("article")->where('id',$id)->setInc('views');
		$info["create_time"] = !empty($info["create_time"])?date('Y-m-d H:i',$info["create_time"]):'';
		$info["keyword"] = explode('|',$info['keyword']);
		$info["comment_count"] = 0;
		//$info["hot_list"] = Db::name("article")->field('id,title')->where('is_tui',1)->where('status',1)->order('id desc')->limit(10)->select();
		$info["hot_list"] = Db::name("article")->field('id,title')->where('status',1)->order('views desc,id desc')->limit(10)->select();
		$info['is_collect'] = 0;
        $user_id = $this->user_id;
		if(!empty($user_id)){
			$flag = Db::name("article_collect")->where('user_id',$user_id)->where('article_id',$id)->value('id');
			if(!empty($flag)){
				$info['is_collect'] = 1;
			}
		}
		return $this->show(1,"成功", $info);
		
    }
	
	
	public function hot_ranking(){
		$list = Db::name("article")->field('id,title')->where('status',1)->order('views desc,id desc')->limit(10)->select();
		
		return $this->show(1,"成功", $list);
	}
	
    
}